package com.loongtech.bi.action.bipay;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.entity.pay.EntityNewUserLTV;
import com.loongtech.bi.manager.pay.UserPayManager;
import com.loongtech.bi.manager.pay.UserPayManager.UserPayCompareData;
import com.loongtech.bi.manager.pay.UserPayManager.UserPayData;
import com.loongtech.bi.manager.pay.UserPayManager.UserPayValueData;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 付费分析 —新玩家价值Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("session")
@RequestMapping("/pay/userPay")
public class UserPayBiAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	public final static String platformCompareTab = "platform";  // 新玩家价值对比标签页-平台
	public final static String channelCompareTab = "channel";  // 新玩家价值对比标签页-渠道
	public final static String serverCompareTab = "server";  // 新玩家价值对比标签页-警服
	public String compareTab = platformCompareTab;  // 当前选中的新玩家价值对比标签页，默认是平台
	
	private List<EntityNewUserLTV> allUserPayValueData;  // 所有数据，用于组织下面的数据
	private List<UserPayData> userPayData;  // 前端玩家价值总体数据 
	private ChartUIData userPayChart;  // 前端新玩家价值总体数据图表
	private List<UserPayValueData> userPayValueData;  // 前端新玩家价值数据
	private ChartUIData userPayValueChart;  // 前端新玩家价值图表
	private List<UserPayCompareData> userPayCompareData;  // 前端新玩家价值对比数据
	private ChartUIData userPayCompareChart;  // 前端新玩家价值对比图表
    private List<UserPayCompareData> userPayCompareData1;  // 前端新玩家价值对比数据
    private ChartUIData userPayCompareChart1;  // 前端新玩家价值对比图表
    private List<UserPayCompareData> userPayCompareData2;  // 前端新玩家价值对比数据
    private ChartUIData userPayCompareChart2;  // 前端新玩家价值对比图表
	
	@Autowired
	private UserPayManager userPayManager;

    @RequestMapping(value = "initParam.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void initParam(HttpServletRequest request, HttpServletResponse response, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }


    @RequestMapping(value = "search.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, @RequestParam String platform, @RequestParam String channel, @RequestParam String server, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            dateStart = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            dateEnd = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);

            queryPlatform = platform;
            queryChannel = channel;
            queryServer = server;

            userPayData = new ArrayList<>();
            userPayChart = new ChartUIData();
            userPayValueData = new ArrayList<>();
            userPayValueChart = new ChartUIData();
            userPayCompareData = new ArrayList<>();
            userPayCompareChart = new ChartUIData();
            userPayCompareData1 = new ArrayList<>();
            userPayCompareChart1 = new ChartUIData();
            userPayCompareData2 = new ArrayList<>();
            userPayCompareChart2 = new ChartUIData();

            allUserPayValueData = userPayManager.getAllUserPayValueData(dateStart, dateEnd);  // 查出所有结果
            if (allUserPayValueData != null && allUserPayValueData.size() > 0) {
                // 更新汇总和详细数据
                userPayManager.updateData(allUserPayValueData, dateStart, dateEnd, queryPlatform, queryChannel, queryServer, userPayData, userPayChart, userPayValueData, userPayValueChart);
                if (userPayChart.isNull()) {
                    userPayChart = null;
                }
                if (userPayValueChart.isNull()) {
                    userPayValueChart = null;
                }

                // 更新对比数据
                userPayManager.updateData2(platformCompareTab, allUserPayValueData, dateStart, dateEnd, queryPlatform, queryChannel, queryServer, userPayCompareData, userPayCompareChart);
                if (userPayCompareChart.isNull()) {
                    userPayCompareChart = null;
                }
                userPayManager.updateData2(channelCompareTab, allUserPayValueData, dateStart, dateEnd, queryPlatform, queryChannel, queryServer, userPayCompareData1, userPayCompareChart1);
                if (userPayCompareChart1.isNull()) {
                    userPayCompareChart1 = null;
                }
                userPayManager.updateData2(serverCompareTab, allUserPayValueData, dateStart, dateEnd, queryPlatform, queryChannel, queryServer, userPayCompareData2, userPayCompareChart2);
                if (userPayCompareChart2.isNull()) {
                    userPayCompareChart2 = null;
                }
            } else {
                userPayChart = null;
                userPayValueChart = null;
                userPayCompareChart = null;
                userPayCompareChart1 = null;
                userPayCompareChart2 = null;
            }

            result.put("res", "0");
            result.put("userPayData", userPayData);
            result.put("userPayChart", userPayChart);
            result.put("userPayValueData", userPayValueData);
            result.put("userPayValueChart", userPayValueChart);
            result.put("userPayCompareData", userPayCompareData);
            result.put("userPayCompareChart", userPayCompareChart);
            result.put("userPayCompareData1", userPayCompareData1);
            result.put("userPayCompareChart1", userPayCompareChart1);
            result.put("userPayCompareData2", userPayCompareData2);
            result.put("userPayCompareChart2", userPayCompareChart2);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
	}
	
	// 切换新玩家价值对比标签页
    @RequestMapping(value = "updateCompareType.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateCompareType(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, @RequestParam String platform, @RequestParam String channel, @RequestParam String server, @RequestParam String compareType, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            dateStart = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            dateEnd = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);

            queryPlatform = platform;
            queryChannel = channel;
            queryServer = server;

            if (compareType != null && compareType.startsWith(channelCompareTab)) {
                compareTab = channelCompareTab;
            } else if (compareType != null && compareType.startsWith(serverCompareTab)) {
                compareTab = serverCompareTab;
            } else {
                compareTab = platformCompareTab;
            }

            allUserPayValueData = userPayManager.getAllUserPayValueData(dateStart, dateEnd);  // 查出所有结果

            userPayCompareData = new ArrayList<>();
            userPayCompareChart = new ChartUIData();
            userPayManager.updateData2(compareTab, allUserPayValueData, dateStart, dateEnd, queryPlatform, queryChannel, queryServer, userPayCompareData, userPayCompareChart);
            if (userPayCompareChart.isNull()) {
                userPayCompareChart = null;
            }

            result.put("res", "0");
            result.put("userPayCompareData", userPayCompareData);
            result.put("userPayCompareChart", userPayCompareChart);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
    }

    @RequestMapping(value = "updateServer.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateServer(HttpServletRequest request, HttpServletResponse response, String server, String callback) {
        try {
            queryServer = server;
            selectedServer();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updateChannel.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateChannel(HttpServletRequest request, HttpServletResponse response, String channel, String callback) {
        try {
            queryChannel = channel;
            updateServerItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updatePlatform.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updatePlatform(HttpServletRequest request, HttpServletResponse response, String platform, String callback) {
        try {
            queryPlatform = platform;
            updateChannelItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @Override
    public void search() {
    }

    @Override
	protected String csvContent() {
		return null;
	}

	@Override
	protected String csvFileName() {
		return null;
	}

	public List<EntityNewUserLTV> getAllUserPayValueData() {
		return allUserPayValueData;
	}

	public void setAllUserPayValueData(List<EntityNewUserLTV> allUserPayValueData) {
		this.allUserPayValueData = allUserPayValueData;
	}

	public List<UserPayData> getUserPayData() {
		return userPayData;
	}

	public void setUserPayData(List<UserPayData> userPayData) {
		this.userPayData = userPayData;
	}

	public List<UserPayValueData> getUserPayValueData() {
		return userPayValueData;
	}

	public void setUserPayValueData(List<UserPayValueData> userPayValueData) {
		this.userPayValueData = userPayValueData;
	}

	public ChartUIData getUserPayValueChart() {
		return userPayValueChart;
	}

	public void setUserPayValueChart(ChartUIData userPayValueChart) {
		this.userPayValueChart = userPayValueChart;
	}

	public List<UserPayCompareData> getUserPayCompareData() {
		return userPayCompareData;
	}

	public void setUserPayCompareData(List<UserPayCompareData> userPayCompareData) {
		this.userPayCompareData = userPayCompareData;
	}

	public ChartUIData getUserPayCompareChart() {
		return userPayCompareChart;
	}

	public void setUserPayCompareChart(ChartUIData userPayCompareChart) {
		this.userPayCompareChart = userPayCompareChart;
	}

	public ChartUIData getUserPayChart() {
		return userPayChart;
	}

	public void setUserPayChart(ChartUIData userPayChart) {
		this.userPayChart = userPayChart;
	}

    public List<UserPayCompareData> getUserPayCompareData1() {
        return userPayCompareData1;
    }

    public void setUserPayCompareData1(List<UserPayCompareData> userPayCompareData1) {
        this.userPayCompareData1 = userPayCompareData1;
    }

    public ChartUIData getUserPayCompareChart1() {
        return userPayCompareChart1;
    }

    public void setUserPayCompareChart1(ChartUIData userPayCompareChart1) {
        this.userPayCompareChart1 = userPayCompareChart1;
    }

    public List<UserPayCompareData> getUserPayCompareData2() {
        return userPayCompareData2;
    }

    public void setUserPayCompareData2(List<UserPayCompareData> userPayCompareData2) {
        this.userPayCompareData2 = userPayCompareData2;
    }

    public ChartUIData getUserPayCompareChart2() {
        return userPayCompareChart2;
    }

    public void setUserPayCompareChart2(ChartUIData userPayCompareChart2) {
        this.userPayCompareChart2 = userPayCompareChart2;
    }
}
